import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/pages/login_page.dart';
import 'package:material_collection_app/utils/dio_utils.dart';
import 'package:material_collection_app/utils/utils.dart';

class HomeUserInfo extends StatelessWidget {
  final Map userInfo;
  final bool loadSuccess;

  HomeUserInfo({Key key, this.userInfo, this.loadSuccess}) : super(key: key);

  Widget _buildAvatar() { 
    if(loadSuccess) {
      if(userInfo['avatar'].toString().isNotEmpty) {
        return Image.network(
          DioUtils.API_PREFIX+userInfo['avatar'], width: 120.w, height: 120.w, fit: BoxFit.cover
        );
      }else {
        return Image.asset('assets/avatar.png', width: 120.w, height: 120.w);
      }
    }else {
      return Container(
        width: 120.w, 
        height: 120.w,
        color: Utils.hexToColor('#F1F3F4'),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
    final double statusBarHeight = MediaQuery.of(context).padding.top;

    print(userInfo);
    
    return Stack(
      children: <Widget>[
        Container(
          margin: EdgeInsets.fromLTRB(30.w, statusBarHeight + 18.w, 30.w, 0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              InkWell(
                onTap: () {
                  Navigator.pushNamed(context, "/my");
                },
                child: Hero(
                  tag: 'avatar',
                  child: Container(
                    width: 104.w,
                    height: 104.w,
                    decoration: BoxDecoration(
                      border: new Border.all(color: Color(0xFF7EB0FF), width: 2),
                      borderRadius: BorderRadius.circular(104.w),
                    ),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(104.w),
                      child: _buildAvatar(),
                    )
                  ),
                )
              ),
              InkWell(
                onTap: () {
                  Utils.openAlert('确定退出当前账号吗？', context)
                  .then((confirm) {
                    if(confirm) {
                      Utils.logOut();
                      Navigator.of(context).pushAndRemoveUntil(
                        MaterialPageRoute(builder: (context) => LoginPage()), (route) => route == null
                      );
                    }
                  });
                },
                child: Container(
                  width: 130.w,
                  height: 100.w,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Image.asset('assets/switch_ac.png',
                        width: 36.w
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 8.w),
                        child: Text('切换账号', 
                          style: TextStyle(color: Colors.white, fontSize: 10, height: 1),
                        ),
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ), 
        Container(
          margin: EdgeInsets.fromLTRB(31.w, 194.w, 0, 15.w),
          child: Text(userInfo['name'] != '' ? ('欢迎您！' + userInfo['name']) : '',
            maxLines: 1,
            style: TextStyle(
              color: Colors.white,
              fontSize: 36.w
            ),
          ),
        )
      ],
    );
  }
}